<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="入库单号">
              <a-input v-model="queryParam.inboundNo" placeholder="请输入入库单号" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单号">
              <a-input v-model="queryParam.orderNo" placeholder="请输入订单号" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓库">
              <j-select-warehouse v-model="queryParam.warehouseId" placeholder="请选择仓库" allow-clear></j-select-warehouse>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="入库状态">
              <j-dict-select-tag v-model="queryParam.inboundStatus" dictCode="finished_goods_inbound_status" placeholder="请选择状态" allow-clear></j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="入库日期">
              <a-range-picker v-model="queryParam.dateRange" format="YYYY-MM-DD" @change="onDateChange"></a-range-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleCreate" type="primary" icon="plus">新增入库单</a-button>
      <a-button @click="handleBatchDelete" type="primary" icon="delete" :disabled="selectedRowKeys.length <= 0">批量删除</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a> 项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="rowSelection"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical"/>
          <a @click="handleEdit(record)" v-if="record.inboundStatus === 0">编辑</a>
          <a-divider type="vertical" v-if="record.inboundStatus === 0"/>
          <a @click="handleExecute(record)" v-if="record.inboundStatus === 0">执行入库</a>
          <a-divider type="vertical" v-if="record.inboundStatus === 0"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)" v-if="record.inboundStatus === 0">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <!-- 入库单详情模态框 -->
    <inbound-detail-modal ref="inboundDetailModal" @ok="handleRefresh"></inbound-detail-modal>
    <!-- 创建入库单模态框 -->
    <inbound-create-modal ref="inboundCreateModal" @ok="handleRefresh"></inbound-create-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import InboundDetailModal from './modules/InboundDetailModal'
import InboundCreateModal from './modules/InboundCreateModal'
import JSelectWarehouse from '@/components/jeecgbiz/JSelectWarehouse'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import { executeInbound, batchDeleteFinishedGoodsInbound } from '@/api/wms/finishedGoodsInbound'

export default {
  name: 'InboundList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    InboundDetailModal,
    InboundCreateModal,
    JSelectWarehouse,
    JDictSelectTag
  },
  data() {
    return {
      description: '成品入库管理页面',
      // 选中的行
      selectedRowKeys: [],
      selectedRows: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '入库单号',
          align: 'center',
          dataIndex: 'inboundNo'
        },
        {
          title: '订单号',
          align: 'center',
          dataIndex: 'orderNo'
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerName'
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName'
        },
        {
          title: '仓库',
          align: 'center',
          dataIndex: 'warehouseName'
        },
        {
          title: '入库数量',
          align: 'center',
          dataIndex: 'totalQuantity'
        },
        {
          title: '入库状态',
          align: 'center',
          dataIndex: 'inboundStatusName'
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'inboundDate'
        },
        {
          title: '操作员',
          align: 'center',
          dataIndex: 'operatorName'
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 200,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/wms/finished-goods-inbound/list',
        delete: '/wms/finished-goods-inbound/delete',
        deleteBatch: '/wms/finished-goods-inbound/deleteBatch'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    },
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.getSuperFieldList()
  },
  methods: {
    initDictConfig() {
      // 初始化字典配置
    },
    getSuperFieldList() {
      // 获取字段配置
    },
    handleCreate() {
      this.$refs.inboundCreateModal.show()
    },
    handleDetail(record) {
      this.$refs.inboundDetailModal.show(record)
    },
    handleEdit(record) {
      this.$refs.inboundCreateModal.show(record)
    },
    handleExecute(record) {
      this.$confirm({
        title: '确认执行入库',
        content: '确定要执行此入库单吗？执行后将生成库存记录并更新订单状态。',
        onOk: () => {
          executeInbound(record.id).then((res) => {
            if (res.success) {
              this.$message.success('入库执行成功')
              this.loadData()
            } else {
              this.$message.error(res.message || '入库执行失败')
            }
          }).catch((err) => {
            this.$message.error('入库执行失败：' + err.message)
          })
        }
      })
    },
    onDateChange(date, dateString) {
      this.queryParam.startDate = dateString[0]
      this.queryParam.endDate = dateString[1]
    },
    handleBatchDelete() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择要删除的记录')
        return
      }
      this.$confirm({
        title: '确认删除',
        content: `确定要删除选中的 ${this.selectedRowKeys.length} 条记录吗？`,
        onOk: () => {
          batchDeleteFinishedGoodsInbound({ ids: this.selectedRowKeys.join(',') }).then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              this.selectedRowKeys = []
              this.selectedRows = []
              this.loadData()
            } else {
              this.$message.error(res.message || '删除失败')
            }
          }).catch((err) => {
            this.$message.error('删除失败：' + err.message)
          })
        }
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    searchReset() {
      this.queryParam = {}
      this.loadData()
    },
    handleRefresh() {
      this.loadData()
    }
  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>
